package com.weiyi.mashangzhu.Roomer.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.MainActivity_R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by win7 on 2017/8/18.
 */

public class Fragment_Order extends Fragment {

    @BindView(R.id.tabs_indicator)
    View tabsIndicator;
    @BindView(R.id.order_page)
    ViewPager orderPage;
    Unbinder unbinder;
    @BindView(R.id.tab_FindFragment_title)
    TabLayout tabFindFragmentTitle;
    private String[] titles = {"全部", "待接单", "待付款", "待入住", "入住中", "待评价","已评价", "退款中", "已退款"};
    private int position = 0;
    private MainActivity_R mainActivity;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivity = (MainActivity_R) getActivity();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
        //tab的标题来自于adapter
        tabFindFragmentTitle.setTabsFromPagerAdapter(adapter);
        orderPage.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabFindFragmentTitle));
        //tabLayout 和 viewpager关联
        tabFindFragmentTitle.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //tab选中时的回调
                position = tab.getPosition();
                orderPage.setCurrentItem(position, true);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        orderPage.setOffscreenPageLimit(titles.length - 1);
        orderPage.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (orderPage != null) {
            orderPage.setCurrentItem(position, true);
            
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        unbinder.unbind();
    }


    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        //返回标题
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public Fragment getItem(int position) {
//           {"全部","待接单","待付款","待入住","入住中","待评价","退款中","已退款"};
            int status = 0;
            if (titles[position].equals("全部")) {
                status = 9;
            } else if (titles[position].equals("待接单")) {
                status = 0;
            } else if (titles[position].equals("待付款")) {
                status = 1;
            } else if (titles[position].equals("待入住")) {
                status = 2;
            } else if (titles[position].equals("入住中")) {
                status = 3;
            } else if (titles[position].equals("待评价")) {
                status = 4;
            }else if (titles[position].equals("已评价")) {
                status = 10;
            } else if (titles[position].equals("退款中")) {
                status = 5;
            } else if (titles[position].equals("已退款")) {
                status = 6;
            }
            //返回fragment 带标题
            NewsFragment_R f = new NewsFragment_R();
            Bundle bundle = new Bundle();
            bundle.putInt("title", status);
            f.setArguments(bundle);
            return f;
        }

        @Override
        public int getCount() {
            return titles.length;
        }
    }
}
